package com.mall.malladminbackend.controller;

import com.mall.malladminbackend.dto.Create;
import com.mall.malladminbackend.dto.Param;
import com.mall.malladminbackend.entity.Role;
import com.mall.malladminbackend.model.R;
import com.mall.malladminbackend.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Date;

/**
 * 作者：郭立仁
 * 描述：永无BUG
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping
    public R all(){
        return roleService.all();
    }

    @PutMapping
    public R update(@RequestBody Role role, Principal principal){
        Create create = new Create(principal.getName(), new Date());
        return roleService.update(role,create);
    }

    @PostMapping
    public R insert(@RequestBody Role role, Principal principal){
        String name = principal.getName();
        Date date = new Date();
        Create create = new Create(name, date, name, date);
        return roleService.insert(role,create);
    }

    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return roleService.delete(id);
    }

    @GetMapping("{id}")
    public R selectMenu(@PathVariable Integer id){
        return roleService.selectMenu(id);
    }

    @PutMapping("menu")
    public R updateRoleAndMenu(@RequestBody Param param,Principal principal){
        String name = principal.getName();
        Date date = new Date();
        Create create = new Create(name, date, name, date);
        return roleService.updateMenu(param.getId(),param.getRoleList(),create);
    }

    //全选
    @GetMapping("all")
    public R allChecked(){
        return roleService.allchecked();
    }
}
